`timescale 1ns / 1ps

module ParticleCounter_tb;

    // 参数定义
    parameter THRESHOLD_HIGH = 16'd10000;
    parameter THRESHOLD_LOW = 16'd5000;
    parameter COUNT_HIGH_CYCLES = 250;
    parameter COUNT_LOW_CYCLES = 250;

    // 信号声明
    reg clk;
    reg rst_n;
    reg clear;
    reg [15:0] adc_data;
    wire [31:0] count;

    // 被测模块实例化
    ParticleCounter #(
        .THRESHOLD_HIGH(THRESHOLD_HIGH),
        .THRESHOLD_LOW(THRESHOLD_LOW),
        .COUNT_HIGH_CYCLES(COUNT_HIGH_CYCLES),
        .COUNT_LOW_CYCLES(COUNT_LOW_CYCLES)
    ) uut (
        .clk(clk),
        .rst_n(rst_n),
        .start(1'b1),
        .clear(clear),
        .adc_data(adc_data),
        .count(count)
    );

    // 时钟生成
    always #5 clk = ~clk; // 100MHz 时钟（周期 10ns）

    integer i;
    // 定义任务
    task simulate_adc;
        input integer cycles; // 循环次数
        begin
            for (i = 0; i < cycles; i = i + 1) begin
                adc_data = 16'd8000;  // 未达到 THRESHOLD_HIGH
                #3000;
                adc_data = 16'd11000; // 连续高于 THRESHOLD_HIGH，计数+1
                #50000;
                adc_data = 16'd3000;  // 连续低于 THRESHOLD_LOW
                #300
                adc_data = 16'd12000;  // 未达到 THRESHOLD_LOW
                #3000;
                adc_data = 16'd3000;  // 连续低于 THRESHOLD_LOW
                #3000;
            end
        end
    endtask


    // 测试程序
    initial begin
        // 初始化信号
        clk = 0;
        rst_n = 0;
        clear = 0;
        adc_data = 16'd0;

        // 复位模块
        #20 rst_n = 1;


        simulate_adc(10);


        // 清零信号
        #10 clear = 1;
        #10 clear = 0;

        // 停止仿真
        #100 $finish;
    end
endmodule
